package com.easyioc.context;

import java.io.File;
import java.util.Collection;

import com.easyioc.config.BeanDefinition;
import com.easyioc.config.ConfigurationReader;
import com.easyioc.support.BeanBuilder;

public abstract class AbstractBeanContext implements BeanContext, ConfigurableBeanContext {

	@Override
	public void refresh() {
		Collection<File> configFiles = loadConfigFiles();
		BeanDefinition[] beanDefs = createConfigurationReader().read(configFiles);
		BeanBuilder beanBuilder = createBeanBuilder();
		buildBeans(beanBuilder, beanDefs);
	}

	/**
	 * This method should be overridden by subclass to decide how to load the
	 * configuration files.
	 */
	protected abstract Collection<File> loadConfigFiles();

	/**
	 * Subclass overrides this method to decide the type of
	 * <code>ConfigurationLoader</code>
	 * 
	 * @return
	 */
	protected abstract ConfigurationReader createConfigurationReader();

	protected abstract BeanBuilder createBeanBuilder();

	/**
	 * This method will build the bean context with a {@link BeanBuilder} and
	 * {@link BeanDefinition}s.
	 * 
	 * @param beanBuilder
	 * @param beanDefs
	 */
	protected abstract void buildBeans(BeanBuilder beanBuilder, BeanDefinition[] beanDefs);
}
